﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using Wicresoft.JDPostBoard.Entity;

public partial class PostBoard_NewPostDetail : BaseAuthenticationPage
{
    protected string PostID
    {
        get
        {
            if (ViewState["PostID"] == null)
            {
                if (Request["PostID"] == null || Request["PostID"] == "")
                    ViewState["PostID"] = Guid.NewGuid().ToString();
                else
                    ViewState["PostID"] = Request["PostID"];
            }

            return ViewState["PostID"].ToString();
        }
    }

    protected string OpenType
    {
        get
        {
            return Request["Type"];
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (OpenType == "Add")
                this.Title = "提交留言";
            else
                this.Title = "编辑留言";
            LoadDataFromEntity();
        }
    }


    protected void LoadDataFromEntity()
    {
        Post post = new Post(new Guid(PostID));
        post.Load();
        this.tbPostTitle.Text = post.PostTitle;
        this.tbPostContent.Text = post.PostContent;
        this.tbLinkAddress.Text = post.LinkAddress;
    }

    protected void lbtnSave_Click(object sender, EventArgs e)
    {
        SaveDataByEntity();
    }

    protected void SaveDataByEntity()
    {
        Post post = new Post();
        post.ID = new Guid(PostID);
        if (!post.Load("ID='" + PostID + "'"))
        {
            post.PostTime = DateTime.Now;
            post.PosterID = CurrentUser.ID.ToString();
            post.PosterName = CurrentUser.Name;
            post.PosterDsName = CurrentUser.DisplayName;
            post.IsDelete = false;
            post.IsReplay = false;
        }
        post.IsRead = false;
        post.PostTitle = this.tbPostTitle.Text;
        post.PostContent = this.tbPostContent.Text;
        post.LinkAddress = this.tbLinkAddress.Text;
        post.Save();

        string script = "<script>alert('保存成功!');refreshParent();window.close();</script>";
        Page.RegisterStartupScript("refresh", script);
    }
}
